#include "include.h"
#include "wireless.h"


struct wireless_cb_tag wireless_cb;


///获取蓝牙连接状态
///return  0：无连接 1：设备1连接  2：设备2连接  3：设备1 2同时连接
AT(.com_text.bsp.wireless_cb)
u8 wireless_get_status(void)
{
    return (wireless_cb.connected_sta);
}
#define MIC_DET_PIN 7  //PA7
AT(.text.bsp.wireless_cb)
void wireless_mic_ctl(u8 onoff)
{
	GPIOADE|= BIT(MIC_DET_PIN);
	GPIOADIR&= ~BIT(MIC_DET_PIN);
	if(onoff)
	{
		GPIOASET= BIT(MIC_DET_PIN);
	}
	else
	{
		GPIOACLR= BIT(MIC_DET_PIN);
	}
	
}
AT(.text.bsp.wireless_cb)
void wireless_emit_notice(uint evt, void *params)
{
//    u32 tmp;
    u8 *packet = params;
    u8 mic_num;

    switch(evt) {
    case BT_NOTICE_WIRELESS_CONNECTED:
        mic_num = packet[0];
        if(mic_num < WIRELESS_CON_LINK_NB) {
            printf("WIRELESS_CONNECTED, %d\n", mic_num);
            sys_cb.led_sta = LED_USER_CONN;
            if(wireless_cb.connected_sta == 0) {
                sys_clk_req(INDEX_DECODE, ALG_SYS_CLK_SEL);     //先抬高主频
                if(wireless_role_is_adapter()) {
					wireless_mic_ctl(1);
                    wireless_adapter_init();
                } else {
                    wireless_device_init();
                }
                wireless_cb.alg_en = 1;                         //再使能算法
            }
            wireless_cb.connected_sta |= BIT(mic_num);
            wireless_cb.change_sta[mic_num] = 0;
            wireless_cb.change_flag = 1;
        }
        break;

    case BT_NOTICE_WIRELESS_CONNECT_FAIL:
        mic_num = packet[0];
        if(mic_num < WIRELESS_CON_LINK_NB) {
            printf("WIRELESS_CONNECT_FAIL, %d\n", mic_num);
            sys_cb.led_sta = LED_USER_CLEAR_PAIR;
            wireless_cb.change_sta[mic_num] = 1;
            wireless_cb.change_flag = 1;
        }
        break;

    case BT_NOTICE_WIRELESS_DISCONNECT:
        mic_num = packet[0];
        if(mic_num < WIRELESS_CON_LINK_NB) {
            printf("WIRELESS_DISCONNECT, %d\n", mic_num);
			printf("1111111\n");
            sys_cb.led_sta = LED_USER_CLEAR_PAIR;
            wireless_cb.connected_sta &= ~BIT(mic_num);
            wireless_cb.change_sta[mic_num] = 2;
            wireless_cb.change_flag = 1;

            if(wireless_cb.connected_sta == 0) {
                wireless_cb.alg_en = 0;                 //先关闭算法
                if(wireless_role_is_adapter()) {
					wireless_mic_ctl(0);
                    wireless_adapter_exit(mic_num);
                } else {
                    wireless_device_exit();
                }
                sys_clk_free(INDEX_DECODE);             //再还原主频
            } else {
                wireless_adapter_exit(mic_num);
            }
        }
        break;

    default:
        break;
    }
}


void wireless_rx_user_cmd(u8 index, u8 *ptr, u8 len)
{
//    printf("user_cmd: %d\n", index);
//    print_r(ptr, len);
    if (wireless_role_is_adapter()){
		printf("prt[0]:%d\n",ptr[0]);
        if(ptr[0] == 0x0f){
            if(index){
                msg_enqueue(EVT_ADAPTER_DISCON1_STA);
            }else{
                msg_enqueue(EVT_ADAPTER_DISCON0_STA);
            }

        }
		else if(ptr[0] == 0x10)
		{
			if(ptr[1]==1)
			{
				msg_enqueue(EVT_DAC_VOL_ADD);
			}
			else
			{
				msg_enqueue(EVT_DAC_VOL_SUB);
			}
		}
    }
}


AT(.text.bsp.wireless_mic)
void wireless_mic_role_init(void)
{
    if (!WIRELESS_MIC_DET_SELECT) {
        if (xcfg_cb.wireless_adapter_en) {
            cfg_wireless_role = true;
            return;
        } else if (xcfg_cb.wireless_device_en) {
            cfg_wireless_role = false;
            return;
        } else {
            ///都被去掉默认发射端
            cfg_wireless_role = false;
            return;
        }
    }
    //return 0;
    u8 i,cnt;
    bool res = false;
    gpio_t tws_sel_left_gpio;
    gpio_t *g = &tws_sel_left_gpio;
    u8 io_num = WIRELESS_MIC_DET_SELECT;
    //printf("io_num = %d\n",io_num);
    if ((!io_num) || (io_num > IO_MAX_NUM)) {
        return;
    }
    bsp_gpio_cfg_init(&tws_sel_left_gpio, io_num);
    //io sta backup
    u32 bk_de = g->sfr[GPIOxDE];
    u32 bk_pu = g->sfr[GPIOxPU];
    u32 bk_dir = g->sfr[GPIOxDIR];
    //数字IO, 开上拉.
    g->sfr[GPIOxDE] |= BIT(g->num);
    g->sfr[GPIOxPU] |= BIT(g->num);
    g->sfr[GPIOxDIR] |= BIT(g->num);

    i = 6;
    cnt = 0;
    while(i--) {
        delay_ms(5);
        if (!(g->sfr[GPIOx] & BIT(g->num)) ) {   //to GND
            //printf("GND\n");
            cnt++;
        } else {
            //printf("!!!PU\n");
        }
        if (cnt >= 3) {
            res = true;
            break;
        }
    }
    //检测完成, 恢复以前IO状态
    g->sfr[GPIOxDE] =  bk_de;
    g->sfr[GPIOxPU] =  bk_pu;
    g->sfr[GPIOxDIR] = bk_dir;
    //printf("res = %d,i = %d, cnt = %d\n",res,i,cnt);
    cfg_wireless_role = res;
}

extern u32 __code_comm1_vma, __code_comm1_lma, __code_comm1_size;
extern u32 __code_comm2_vma, __code_comm2_lma, __code_comm2_size;
extern u32 __code_adapter_vma, __code_apapter_lma, __code_apapter_size;
extern u32 __code_emit_vma, __code_emit_lma, __code_emit_size;
extern u32 __code_dnn_L1_comm_vma, __code_dnn_L1_comm_lma, __code_dnn_L1_comm_size;

static void wireless_mic_load_code(void)
{
    memcpy(&__code_comm1_vma, &__code_comm1_lma, (u32)&__code_comm1_size);
    memcpy(&__code_comm2_vma, &__code_comm2_lma, (u32)&__code_comm2_size);

    if(xcfg_cb.wireless_adapter_en) {
        memcpy(&__code_adapter_vma, &__code_apapter_lma, (u32)&__code_apapter_size);
        printf("load_code(1): %x, %x, %x\n", &__code_adapter_vma, &__code_apapter_lma, (u32)&__code_apapter_size);
    } else {
        memcpy(&__code_emit_vma, &__code_emit_lma, (u32)&__code_emit_size);
        printf("load_code(0): %x, %x, %x\n", &__code_emit_vma, &__code_emit_lma, (u32)&__code_emit_size);
        #if DNN_L1_EN
        memcpy(&__code_dnn_L1_comm_vma, &__code_dnn_L1_comm_lma, (u32)&__code_dnn_L1_comm_size);
        printf("load_code(2): %x, %x, %x\n", &__code_dnn_L1_comm_vma, &__code_dnn_L1_comm_lma, (u32)&__code_dnn_L1_comm_size);
        #endif // DNN_L1_EN
    }

}

AT(.text.bsp.wireless_mic)
void wireless_mic_var_init(void)
{
    wireless_mic_role_init();
    wireless_mic_load_code();
    wireless_cmd_init();
}

